package com.ss.android.article.base.feature.detail.model;

import com.ss.android.image.model.ImageInfo;
import com.ss.android.model.ForumLink;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

public final class ForumPostData {
    public long id;
    public String content;
    public List imageList;
    public String userName;
    public ForumLink forumLink;
    public String url;

    public ForumPostData() {

    }

    public static ForumPostData extractFromJO(JSONObject jsonObject) {
        if(jsonObject != null) {
            long id = jsonObject.optLong("id");
            if(id > 0) {
                ForumPostData forumPostData = new ForumPostData();
                forumPostData.id = id;
                jsonObject.optString("description");
                jsonObject.optString("avatar_image_url");
                jsonObject.optInt("is_blocked");
                jsonObject.optInt("is_blocking");
                forumPostData.userName = jsonObject.optString("user_name");
                jsonObject.optInt("user_id");
                jsonObject.optBoolean("user_verified");
                forumPostData.content = jsonObject.optString("content");
                JSONObject contentLinkJO = jsonObject.optJSONObject("content_link");
                if(contentLinkJO != null) {
                    forumPostData.url = contentLinkJO.optString("url");
                }

                forumPostData.forumLink = ForumLink.fromJO(jsonObject.optJSONObject("forum_link"));
                JSONArray imageListJO = jsonObject.optJSONArray("image_list");
                ArrayList<ImageInfo> imageInfoList = null;
                if(imageListJO != null) {
                    ArrayList<ImageInfo> tempImageInfoList = new ArrayList(imageListJO.length());
                    for(int i = 0; i < imageListJO.length(); ++i) {
                        JSONObject jo = imageListJO.optJSONObject(i);
                        if(jo != null) {
                            ImageInfo imageInfo = ImageInfo.fromJson(jo, true);
                            if(imageInfo != null) {
                                tempImageInfoList.add(imageInfo);
                            }
                        }
                    }
                    imageInfoList = tempImageInfoList;
                }

                if(imageInfoList != null && imageInfoList.size() > 0) {
                    forumPostData.imageList = imageInfoList;
                }
                return forumPostData;
            }
        }
        return null;
    }
}

